﻿
Imports remotecall
Imports centrosphere.gravitation

Public Class gravitation_rc4_star
    Inherits gravitation_star(Of rcserve4, fromBytes(), toBytes())

    Protected Overrides Function create_output(ByVal field_count As Int32) As toBytes()
        Dim rtn(field_count - 1) As toBytes
        Return rtn
    End Function

    Protected Overrides Function create_server(ByVal port As Int32, ByVal connectionCount As Int64) As rcserve4
        Return New rcserve4()
    End Function

    Protected Overrides Sub inject_output_ijob(ByVal output() As toBytes, ByVal job As ijob)
        output(2) = job
    End Sub

    Protected Overrides Sub inject_output_jobid(ByVal output() As toBytes, ByVal index As fromBytes)
        output(1) = index
    End Sub

    Protected Overrides Sub inject_output_result(ByVal output() As toBytes, ByVal result As fromBytes)
        output(0) = result
    End Sub

    Protected Overrides Function parse_input_valid(ByVal input() As fromBytes) As Boolean
        Return Not input Is Nothing AndAlso input.Length() = 3
    End Function

    Protected Overrides Function parse_input_ijob(ByVal input() As fromBytes, ByRef o As ijob) As Boolean
        Return input(2).cast(Of ijob)(o)
    End Function

    Protected Overrides Function parse_input_jobId(ByVal input() As fromBytes, ByRef o As bytesInt64) As Boolean
        Return input(1).cast(Of bytesInt64)(o)
    End Function

    Protected Overrides Function parse_input_queuename(ByVal input() As fromBytes, ByRef o As bytesString) As Boolean
        Return input(0).cast(Of bytesString)(o)
    End Function

    Protected Overrides Sub register(ByVal server As rcserve4,
                                     ByVal name As String,
                                     ByVal callback As _do(Of fromBytes(), pointer(Of toBytes()), eventComb))
        assert(server.register(name, callback))
    End Sub

    Protected Overrides Function start_server(ByVal server As rcserve4,
                                              ByVal port As Int32,
                                              ByVal connectionCount As Int64) As Boolean
        server.connectionCount() = connectionCount
        Return server.start(port)
    End Function

    Protected Overrides Sub stop_server(ByVal server As rcserve4)
        server.stop()
    End Sub
End Class

Public Class gravitation_rc4_planet
    Inherits gravitation_planet(Of rcclient4, fromBytes())

    Protected Overrides Function create_client(ByVal host As String, ByVal port As Int32) As rcclient4
        Return New rcclient4(host, port)
    End Function

    Protected Overrides Function invoke(ByVal client As rcclient4,
                                        ByVal name As String,
                                        ByRef ec As eventComb,
                                        ByVal rtn As pointer(Of fromBytes()),
                                        ByVal ParamArray paras() As toBytes) As Boolean
        assert(Not client Is Nothing)
        Return client.invoke(name, ec, rtn, paras)
    End Function

    Protected Overrides Function parse_return_job(ByVal r() As fromBytes, ByRef o As ijob) As Boolean
        Return r(2).cast(Of ijob)(o)
    End Function

    Protected Overrides Function parse_return_sourceIndex(ByVal r() As fromBytes, ByRef o As bytesInt64) As Boolean
        Return r(1).cast(Of bytesInt64)(o)
    End Function

    Protected Overrides Function parse_return_success(ByVal r() As fromBytes, ByRef o As bytesBoolean) As Boolean
        Return r(0).cast(Of bytesBoolean)(o)
    End Function

    Protected Overrides Function parse_return_valid(ByVal r() As fromBytes) As Boolean
        Return Not r Is Nothing AndAlso (r.Length() = 3 OrElse r.Length() = 1)
    End Function
End Class
